﻿module Sustainalytics.CompanyOverview {

    import Common = Sustainalytics.Common;
    import SearchPortfolio = Sustainalytics.Portfolio.PortfolioSearchController;

    // Portfolio List controller
    export class CompanyAddToPortfolioController {

        public portfolio: Sustainalytics.Portfolio.PortfolioDto;
        public portfolioFilter: Sustainalytics.Portfolio.PortfolioFilterDto;
        public isShared: boolean;
        public portfolioSearch: string;
        private accountId: string;
        private userId: string;

        // Ctor.
        constructor(
            private $rootScope: ng.IScope,
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private $location: ng.ILocationService,
            private PortfolioService: Sustainalytics.Portfolio.PortfolioService,
            private PortfolioCompanyService: Sustainalytics.Portfolio.PortfolioCompanyService,
            private notificationFactory: Common.INotificationFactory,
            private $modal: Common.IModal,
            private $modalInstance: Common.IModalInstance,
            private companyId: string,
            private companyText: string) {

            var ctx = this;
            ctx.isShared = false;
            ctx.portfolioFilter = new Sustainalytics.Portfolio.PortfolioFilterDto();
            ctx.portfolioFilter.isNameEqual = true;
            ctx.portfolioFilter.sharingStatus = 1;

            ctx.portfolioSearch = '';

            if (ctx.$rootScope['userInfo'] == null) {
                ctx.$rootScope['userInfo'] = angular.fromJson(localStorage.getItem('sust_userInfo'));
            }
            ctx.accountId = ctx.$rootScope['userInfo'].accountId;
            ctx.userId = ctx.$rootScope['userInfo'].userId;

            $scope['addCompanyToPortfolio'] = ctx;
        }

        public cancel = function () {
            this.$modalInstance.dismiss('cancel');
        };

        public selectPortfolioAdd(item) {
            var ctx = this;
            ctx.portfolioFilter.namePortfolio = item.name;
            ctx.portfolioFilter.portfolioId = item.id;
        }

        public addCompanyToPortfolio(): ng.IPromise<{}> {
            var ctx = this;
            var def = this.$q.defer();
            var portfolioListTemp: Sustainalytics.Portfolio.PortfolioDto[];

            var promiseAddCompanyToPortfolio: ng.IPromise<{}>;
            //if (ctx.portfolioFilter.portfolioId == null)
            //    alert("Selecteaza din drop down un portofoliu!");
            if (ctx.portfolioFilter.portfolioId != null){
                promiseAddCompanyToPortfolio = ctx.PortfolioCompanyService.insertPortfolioCompany(ctx.companyId, ctx.portfolioFilter.portfolioId);
                ctx.cancel();
            }
            
            return promiseAddCompanyToPortfolio;
        }
    }

    // register controller with News Angular module
    angular.module('Sustainalytics.CompanyOverview').controller('CompanyAddToPortfolioController', function ($rootScope: ng.IScope, $scope: ng.IScope, $q: ng.IQService, $location: ng.ILocationService, PortfolioService, PortfolioCompanyService, notificationFactory: Common.INotificationFactory, $modal: Common.IModal, $modalInstance: Common.IModalInstance, companyId: string, companyText: string) {
        var ctrl = new Sustainalytics.CompanyOverview.CompanyAddToPortfolioController($rootScope, $scope, $q, $location, PortfolioService, PortfolioCompanyService, notificationFactory, $modal, $modalInstance, companyId, companyText);
        return ctrl;
    });
} 
